<template>
	<view class="order-wrapper" :style="{paddingTop: paddingTop + 'px'}">
		<navBar title="奖品" />
		<view class="order-tab" :style="{top: paddingTop + 'px'}">
			<u-tabs :list="list" :scrollable="false" :current="current" lineColor="#CF1A1B" lineWidth="55"
				lineHeight="4" :activeStyle="{
	  			color: '#CF1A1B',
				'font-size':'14px'
	  		}" :inactiveStyle="{
	  			color: 'rgba(0,0,0,0.65)',
				'font-size':'14px'
	  		}" itemStyle="height: 44px;" @change="handleTab">
			</u-tabs>
		</view>
		<view v-if="orderList.length == 0" class="empty">
			<image src="@/static/empty.png"></image>
			<text>无数据</text>
		</view>
		<view class="active-tip" :style="{top: paddingTop+44+ 'px'}" v-if="current == 1">
			<view class="tip-info">
				<view class="title">
					<u-icon name="info-circle-fill" color="#F9AE3D" size="30"></u-icon>中奖的用户，我们将通过电话联系您，请注意接听
				</view>
				<view class="btn">
					<u-button type="warning" text="活动详情" size="small" @click="goActionDetail">
					</u-button>
				</view>
			</view>
		</view>
		<view class="list-wrapper" v-if="current == 0">
			<view class="order-list" v-for="item in orderList">
				<view class="order-item" @click.stop="onDetail(item)">
					<view class="order-item-h">
						<view class="store-name">
							快递单号：<text class="name">{{ item.trackingNumber?item.trackingNumber:'暂无' }}</text>
							<image
								src="https://coach-vehicle-uat-1313990257.cos.ap-beijing.myqcloud.com/miniapp/order/copy.png"
								v-if="item.trackingNumber" @click.native.stop="onCopy(item.trackingNumber)"></image>
						</view>
						<view class="order-status">
							<text v-if="item.status == 0" style="color:#F55932">待生效</text>
							<text v-if="item.status == 1" style="color:#F68B53">待发货</text>
							<text v-if="item.status == 4" style="color:#1EC48A">已完成</text>
							<text v-if="item.status == 3" style="color:#CF1A1B">待收货</text>
							<text v-if="item.status == 6" style="color:rgba(0,0,0,0.45)">已取消</text>
						</view>
					</view>
					<view class="order-item-c">
						<view class="goods-list">
							<view class="goods-item">
								<view class="goods-img">
									<image class="img"
										:src="item.picUrl?item.picUrl:'https://coach-vehicle-uat-1313990257.cos.ap-beijing.myqcloud.com/miniapp/order/goods_default_square.png'">
									</image>
								</view>
								<view class="goods-info end-goods-info">
									<view class="info">
										<view class="goods-name">{{ item.giftName }}</view>
										<view class="goods-surce">礼品来源：{{ item.sourceName }}</view>
										<view class="goods-date">获取时间：{{ item.createdTime }}</view>
									</view>
								</view>
							</view>
						</view>
					</view>
					<view class="order-item-b">
						<u-button type="primary" text="确认收货" size="small" @click.native.stop="openModal(item)"
							v-if="item.status == 3">
						</u-button>
						<!-- 		<u-button type="primary" text="退款进度" :plain="true" size="small" v-if="item.orderState == 8">
						</u-button> -->
					</view>
				</view>
			</view>
		</view>
		<view class="list-wrapper" v-if="current == 1" :style="{'padding-top':'176rpx'}">
			<view class="lottery-list">
				<view class="lottery-item" v-for="item in orderList">
					<view class="item-left">
						<image v-if="item.winningStatus == 0"
							src="https://coach-vehicle-uat-1313990257.cos.ap-beijing.myqcloud.com/miniapp/guarantee/bg_youhuiquan5.png">
						</image>
						<image v-if="item.winningStatus == 1"
							src="https://coach-vehicle-uat-1313990257.cos.ap-beijing.myqcloud.com/miniapp/guarantee/bg_youhuiquan3.png">
						</image>
						<image v-if="item.winningStatus == 2"
							src="https://coach-vehicle-uat-1313990257.cos.ap-beijing.myqcloud.com/miniapp/guarantee/bg_youhuiquan1.png">
						</image>
						<view class="code">{{ item.raffleCode }}</view>
					</view>
					<view class="item-right">
						<image v-if="item.winningStatus == 0"
							src="https://coach-vehicle-uat-1313990257.cos.ap-beijing.myqcloud.com/miniapp/guarantee/bg_youhuiquan6.png">
						</image>
						<image v-if="item.winningStatus == 1"
							src="https://coach-vehicle-uat-1313990257.cos.ap-beijing.myqcloud.com/miniapp/guarantee/bg_youhuiquan4.png">
						</image>
						<image v-if="item.winningStatus == 2"
							src="https://coach-vehicle-uat-1313990257.cos.ap-beijing.myqcloud.com/miniapp/guarantee/bg_youhuiquan2.png">
						</image>
						<view class="code-info">
							<view class="code-title">{{ item.batchCode }}</view>
							<view class="code-date">
								开奖时间：{{ item.lotteryTime?$u.timeFormat(item.lotteryTime, 'yyyy-mm-dd hh:MM'):'待定' }}
							</view>
							<view class="code-state">
								开奖状态：{{ item.winningStatus == 0?'未开奖': (item.winningStatus == 1?'未中奖':'已中奖')}}</view>
						</view>
					</view>
				</view>
			</view>
		</view>
		<uni-load-more :status="status" v-if="loadVisible" :contentText="contentText" />
		<u-modal :show="show" @confirm="onConfirm" :showCancelButton="true" content="是否收到商品？" @cancel="show=false">
		</u-modal>
	</view>
</template>

<script>
	import {
		mapGetters,
		mapState,
		mapMutations
	} from 'vuex'
	import navBar from '@/components/navBar.vue'
	export default {
		components: {
			navBar,
		},
		options: {
			styleIsolation: 'shared'
		},
		data() {
			return {
				current: 0,
				list: [{
					name: '礼品',
					status: 1
				}, {
					name: '抽奖码',
					status: 2,
				}],
				orderState: '',
				pageNum: 1,
				pageSize: 10,
				orderList: [],
				codeLidt: [],
				total: 0,
				emptyVisible: false,
				status: 'more',
				loadVisible: false,
				contentText: {
					contentdown: "上拉显示更多",
					contentrefresh: "正在加载...",
					contentnomore: "没有更多数据了",
				},
				orderStates: null,
				paddingTop: '',
				show: false,
				currentOrder: ''
			}
		},
		computed: {
			...mapGetters('user', ['getUserInfo']),
		},
		methods: {
			handleTab(e) {
				this.pageNum = 1
				this.orderList = []
				this.current = e.index
				this.initData()

			},
			//获取礼品数据
			async initData() {
				try {
					if (this.current == 0) {
						const res = await this.$request({
							method: 'POST',
							url: '/ltb-center/giftRecords/carOwnerOrders',
							data: {
								pageNum: this.pageNum,
								pageSize: this.pageSize
							}
						})
						this.orderList = this.orderList.concat(res.datas.data)
						this.total = res.datas.count
						this.loadVisible = this.total > this.pageSize
						this.emptyVisible = this.total === 0
						this.status = 'more'
					} else {
						const res = await this.$request({
							method: 'POST',
							url: '/marketing-center/raffleActivityBatchDetailEntity/userRaffleBatchList',
							data: {
								pageNum: this.pageNum,
								pageSize: this.pageSize,
								userId: this.getUserInfo.id,
							}
						})
						this.orderList = this.orderList.concat(res.datas.data)
						this.total = res.datas.count
						this.loadVisible = this.total > this.pageSize
						this.emptyVisible = this.total === 0
						this.status = 'more'
					}
					uni.hideLoading()
				} catch (res) {
					uni.hideLoading()
					uni.showToast({
						title: '请求失败',
						icon: 'error'
					})
				}

				uni.stopPullDownRefresh();
			},
			openModal(val) {
				this.show = true
				this.currentOrder = val
			},
			//确认收货
			async onConfirm() {
				try {
					const res = await this.$request({
						method: 'get',
						url: '/ltb-center/giftRecords/confirm',
						data: {
							id: this.currentOrder.id
						}
					})
					uni.showToast({
						title: '已确认收货',
					})
					this.loadVisible = false
					this.pageNum = 1
					this.orderList = []
					this.initData()
					this.show = false
				} catch (res) {
					uni.showToast({
						title: '请求失败',
						icon: 'error'
					})
				}
			},

			//复制
			onCopy(data) {
				uni.setClipboardData({
					data: data, // 复制的内容 
					success: function() {
						uni.showToast({
							title: '复制成功',
							duration: 1000
						})
					}
				});
			},
			//活动详情
			goActionDetail() {
				uni.navigateTo({
					url: `/minePack/function/raffle/raffle`
				})
			},

			//查看详情
			onDetail(item) {
				uni.navigateTo({
					url: `/indexPack/prize/prizeDetail?id=${item.id}`
				})
			},
		},

		onReachBottom() {
			if (this.orderList.length >= this.total) {
				this.status = 'noMore'
				return
			}
			this.loadVisible = true
			this.status = 'loading'
			this.pageNum++
			this.initData()
		},
		onPullDownRefresh() {
			this.loadVisible = false
			this.pageNum = 1
			this.orderList = []
			this.initData()
		},
		onLoad(option) {
			this.paddingTop = this.$store.getters['global/getSystemInfo'].navBarHeight
			uni.showLoading({
				title: '加载中...'
			})
			//参数status => 订单状态：1礼品 2:抽奖码
			if (option.status) {
				switch (option.status) {
					case '1':
						this.current = 0
						break;
					case '2':
						this.current = 1
						break;
					default:
						break;
				};
			} else {
				this.current = 0

			}
			this.initData()

		},
	}
</script>

<style lang="scss" scoped>
	.order-wrapper {
		width: 100%;
		height: 100%;
		font-size: 14px;
		box-sizing: border-box;
		padding-bottom: env(safe-area-inset-bottom);

		/deep/ .u-tabs__wrapper__nav__line {
			margin-left: 16px;
		}

		/deep/ .u-tabs__wrapper__nav__item {
			padding: 0;
		}

		.empty {
			width: 100%;
			height: 80%;
			position: absolute;
			display: flex;
			flex-direction: column;
			align-items: center;
			justify-content: center;

			image {
				width: 220px;
				height: 180px;
			}

			text {
				font-size: 30rpx;
				color: rgba(0, 0, 0, 0.45);
				line-height: 30rpx;
			}
		}

		.order-tab {
			z-index: 99;
			position: fixed;
			width: 100%;
			top: 0;
			background-color: #FFFFFF;
		}

		.list-wrapper {
			padding-top: 108rpx;
			position: relative;
		}

		.order-list {
			.order-item {
				background: #fff;
				margin-bottom: 20rpx;

				.order-item-h {
					padding: 28rpx 32rpx;
					display: flex;
					justify-content: space-between;
					align-items: center;
					align-content: center;
					border-bottom: 2rpx solid rgba(0, 0, 0, 0.1);

					.store-name {
						display: flex;
						flex: 1;
						color: rgba(0, 0, 0, 0.85);
						width: calc(100% - 200rpx);

						image {
							width: 36rpx;
							height: 36rpx;
							margin-right: 10rpx;
						}

						.name {
							max-width: calc(100% - 46rpx);
							word-wrap: break-all;
							overflow: hidden;
							text-overflow: ellipsis;
							line-break: anywhere;
							display: -webkit-box;
							-webkit-line-clamp: 1;
							text-shadow: 0 0 0.1px currentcolor;
							line-clamp: 1;
							-webkit-box-orient: vertical;
						}
					}

					.order-status {
						width: 200rpx;
						text-align: right;
					}
				}

				.order-item-c {
					padding: 16rpx 26rpx;

					.goods-list {
						border-bottom: 2rpx solid rgba(0, 0, 0, 0.1);
					}

					.goods-item {
						display: flex;
						justify-content: flex-start;
						align-items: center;
						padding: 20rpx 0;

						.goods-img {
							height: 176rpx;
							width: 176rpx;
							margin-right: 24rpx;

							.img {
								width: 100%;
								height: 100%;
							}
						}

						.goods-info {
							flex: 1;
							display: flex;
							height: 180rpx;
							justify-content: flex-start;
							align-items: center;
							width: calc(100% - 200rpx);
							border-bottom: 2rpx solid rgba(0, 0, 0, 0.1);

							.info {
								width: 100%;
							}

							.goods-name {
								font-size: 15px;
								font-weight: 500;
								color: rgba(0, 0, 0, 0.85);
								margin-bottom: 28rpx;
								word-wrap: break-all;
								overflow: hidden;
								line-break: anywhere;
								text-overflow: ellipsis;
								display: -webkit-box;
								-webkit-line-clamp: 2;
								line-clamp: 2;
								-webkit-box-orient: vertical;

							}

							.goods-surce {
								font-size: 13px;
								margin-bottom: 16rpx;
								color: rgba(0, 0, 0, 0.65);
							}

							.goods-date {
								font-size: 13px;
								color: rgba(0, 0, 0, 0.65);
							}
						}

						.end-goods-info {
							border-bottom: none;
						}
					}
				}

				.goods-total {
					display: flex;
					justify-content: flex-end;
					font-size: 14px;
					color: rgba(0, 0, 0, 0.65);
					padding: 32rpx 26rpx;
					padding-top: 20rpx;

					.total-price {
						color: #F83E48;
						font-weight: 400;
					}
				}

				.order-item-b {
					height: auto;
					overflow: hidden;
					padding-bottom: 20rpx;

					/deep/ .u-button {
						width: auto;
						margin-right: 20rpx;
						float: right;
					}
				}
			}
		}

		.lottery-list {
			padding: 0 32rpx;

			.lottery-item {
				display: flex;
				margin-bottom: 20rpx;

				.item-left {
					width: 232rpx;
					height: 190rpx;
					position: relative;
					display: flex;
					align-items: center;
					justify-content: center;

					image {
						width: 100%;
						height: 100%;
						position: absolute;
						z-index: 1;
					}

					.code {
						font-size: 30px;
						font-weight: bold;
						color: #FFFFFF;
						z-index: 2;
					}
				}

				.item-right {
					flex: 1;
					position: relative;
					display: flex;
					align-items: center;

					image {
						width: 100%;
						height: 100%;
						position: absolute;
						z-index: 1;
					}

					.code-info {
						z-index: 2;
						padding-left: 32rpx;

						.code-title {
							font-size: 16px;
							font-weight: 500;
							color: #FFFFFF;
							margin-bottom: 28rpx;
						}

						.code-date {
							font-size: 13px;
							font-weight: 400;
							color: rgba(255, 255, 255, 0.85);
							margin-bottom: 16rpx;
						}

						.code-state {
							font-size: 13px;
							font-weight: 400;
							color: rgba(255, 255, 255, 0.85)
						}
					}
				}
			}
		}

		.active-tip {
			position: fixed;
			top: 0;
			background: red;
			width: 100%;
			height: 76rpx;
			background: #FDF6EC;
			z-index: 4;

			.tip-info {
				display: flex;
				justify-content: space-between;
				align-items: center;
				color: #F9AE3D;
				font-size: 12px;
				line-height: 76rpx;
				padding: 0 10rpx;

				.title {
					display: flex;
				}
			}
		}
	}
</style>
